<?php
/**
 * 用户管理
 */
namespace app\passport\model;
use think\Model;

class User extends Model{

    protected $pk       = 'id';
    protected $table    = 'sp_passport_user';
    protected $autoWriteTimestamp = true;
    protected $pageNumber = 20;

    /**
     * 单个
     * @param int $param
     * @return array
     */
    public function find_info($param) {
        return self::get($param);
    }

    /**
     * 列表
     * @access public
     * @param array $where
     */
    public function select_lists(){
        return $this->order('id desc')->paginate($this->pageNumber);
    }

    /**
     * 列表-数量统计
     * @access public
     * @param array $where
     */
    public function select_lists_counts(){
        return $this->order('id desc')->count();
    }
    
  
    /**
     * 添加
     * @param array $param
     * @return int
     */
    public function add_data($param) {
        $data = [
            'password'=> password_hash(md5($param['password']),PASSWORD_DEFAULT),
            'user_name'  => $param['user_name'],
            'phone_id' => $param['phone_id'],
            'create_time' => time(),
            'update_time' => time(),
            'last_login_time' => time(),
        ];
        return $this->insertGetId($data);
    }
    
    /**
     * 编辑
     * @param type $param
     */
    public function edit_data($param) {
        if(isset($param['password'])){
            $data = [
                'password'=> password_hash(md5($param['password']),PASSWORD_DEFAULT),
                'user_name'  => $param['user_name'],
                'phone_id' => $param['phone_id'],
            ];

        }else{
             $data = [
            'user_name'  => $param['user_name'],
            'phone_id' => $param['phone_id'],
            ];
        }
       
        $where = ['id'=>$param['id']];
        return $this->where($where)->update($data);
    }
    
    /**
     * 锁定用户
     * @param int $id
     */
    public function update_data($param) {
        $data = [
            'locks'=>$param['locks'],
        ];
        $where = ['id'=>$param['id']];
        return $this->where($where)->update($data);
    }
}